var form = document.querySelector('.bot_bot>form')
var username = document.querySelector('[name="username"]')
var password = document.querySelector('[name="password"]')

form.onsubmit = function () {
    var use = /^[a-z0-9]\w{4,11}$/
    if (!use.test(username.value)) {
        alert("用户名不符合规范，应该是字母数字开头，5~12位")
        return false
    }
    var pas = /\w{6,12}/
    if (!pas.test(password.value)) {
        alert("密码不符合规范，应该是字母数字下划线6~12位")
        return false
    }
    var xhr = new XMLHttpRequest;
    xhr.open('post', 'http://localhost:8888/users/login')
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
    xhr.send(`username=${username.value}&password=${password.value}`)
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
                var str = xhr.responseText
                str = JSON.parse(str)
                // console.log(str);
                var { code, message, token, user } = str
                alert(message)
                if (code === 1) {
                    var { id } = user
                    localStorage.setItem('token',token)
                    localStorage.setItem('id',id)
                    location.href = `home.html`
                }
            }
        }
    }
    return false
}